package com.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.KechengxinxiEntity;
import com.entity.ZuoyebuzhiEntity;
import com.entity.excel.ExcelData;
import com.service.KechengxinxiService;
import com.service.ZuoyebuzhiService;
import com.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/kechengxinxi2")
public class Kechengxinxi3Controller {

    @Autowired
    private KechengxinxiService kechengxinxiService;

    @Autowired
    private ZuoyebuzhiService zuoyebuzhiService;

    @GetMapping("/get")
    public ResponseEntity<Resource> get() {
        Resource resource = new ClassPathResource("excel/模板文件.xlsx");
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }

    @PostMapping("/put/{id}")
    public R put(@RequestParam("file") MultipartFile file, @PathVariable Long id) throws IOException {
        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            throw new RuntimeException("仅支持Excel文件(.xlsx, .xls)");
        }

        List<ExcelData> list = EasyExcel.read(file.getInputStream())
                .head(ExcelData.class)
                .sheet()
                .doReadSync();
        List<ExcelData> res = new ArrayList<>();

        KechengxinxiEntity kechengxinxiEntity = kechengxinxiService.selectById(id);
        for (ExcelData excelData : list) {
            ZuoyebuzhiEntity zuoyebuzhiEntity = new ZuoyebuzhiEntity<>();
            BeanUtils.copyProperties(kechengxinxiEntity, zuoyebuzhiEntity);
            BeanUtils.copyProperties(excelData, zuoyebuzhiEntity);

            zuoyebuzhiEntity.setAddtime(new Date());
            zuoyebuzhiEntity.setFaburiqi(new Date());
            zuoyebuzhiEntity.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());

            int count = zuoyebuzhiService.selectCount(new EntityWrapper<ZuoyebuzhiEntity>()
                    .eq("ketimingcheng", zuoyebuzhiEntity.getKetimingcheng()));
            if (count!=0) continue;

            zuoyebuzhiService.insert(zuoyebuzhiEntity);
            excelData.setId(zuoyebuzhiEntity.getId());
            res.add(excelData);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("data", res);
        return R.ok(map);
    }

}